var ClubMemberManagerTab = Item.extend({
    _ccsFileName: res.ClubMemberManagerTab_json,
    _clubId: 0,
    _tableView: null,
    _usersList: [],
    _originalList: [],
    _onlineMemberNum: 0,

    _filterType: 0, //默认的过滤条件
    _filterGroup: -1, //默认显示所有组成员
    initItem: function (_clubId) {
        this._clubId = _clubId;
        this.initTableView();
        // this._initDropDownMenu();
    },
    onEnter: function () {
        Item.prototype.onEnter.call(this);
        this._addClickListeners();
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'nodeTitle.btn_find': '_clickFind',
            'nodeTitle.btn_fenpei': '_clickFenPei',
            'nodeTitle.seeGroup': '_clickSeeGroup',
            'nodeTitle.changeGroup': '_clickChangeGroup',
        }
    },
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubGroupSelectEvent] = '_selectGroupHandler';
    },

    /**
     * 发送当前界面 需要请求的数据消息
     */
    sendMsg: function () {
        clubMsgManager.requestClubMember(this._clubId, true);
        clubMsgManager.requestGroups(this._clubId, true);
    },

    /**
     * 初始化下拉菜单
     * @private
     */
    _initDropDownMenu: function () {
        var config = {
            selectBgRes: res.dropDownMenuTopBack,
            selectBtnRes: res.dropDownMenuBtn,
            menuRes: res.dropDownMenuBack3,
            dataArr: ['全部', '管理员', '在线用户', '离线用户', '禁玩用户'],
            normalIndex: 0,
            fontSize: 20,
            selectCallBack: this._dropDownMenuHandler.bind(this)
        };
        this._dropDownMenu = new DropDownMenu(config);
        this.addChild(this._dropDownMenu);
        this._dropDownMenu.setPosition(this._nodeList['menu'].getPosition());
    },

    /**
     * 下拉菜单选中的内容
     * @param content
     * @param index
     * @private
     */
    _dropDownMenuHandler: function (content, index) {
        this._filterType = index;
        this._membersFilter();
    },

    /**
     * 设置组的合伙人
     * @private
     */
    _getClubPartner: function(clubId){
        var clubInfo = ClubDataManager.getClubData(clubId);
        var groups = clubInfo.groups;
        if (groups == null || groups == undefined) return [];
        var retPartnerUids = [];
        for (var i = 0; i < groups.length; i++){
            if (groups[i].op_uid > 0){
                retPartnerUids.push(groups[i].op_uid);
            }
        }

        return retPartnerUids;
    },

    refreshView: function (clubId) {
        this._clubId = clubId;

        var memberObj = ClubDataManager.getClubMembersSort(clubId);
        this._originalList = memberObj.member;
        this._onlineMemberNum = memberObj.onlineMemberNum;
        //更新合伙人
        var partnerUids = this._getClubPartner(clubId);
        var tmpMember;
        for (var i = 0; i < this._originalList.length; i++) {
            tmpMember = this._originalList[i];
            if (partnerUids.indexOf(tmpMember.uid) > -1){
                tmpMember.partner = true;
            }
        }

        this._membersFilter();
        this._nodeList['nodeTitle.player_num'].setString(this._onlineMemberNum);
        this._nodeList['nodeTitle.total_num'].setString('/' + this._originalList.length);

        if (gameData.uid != ClubDataManager.getClubData(clubId).owner_uid) {
            this._nodeList['nodeTitle.changeGroup'].setVisible(false);
            this._nodeList['nodeTitle.btn_fenpei'].setVisible(false);
        }
    },

    /**
     * 点击查找
     * @private
     */
    _clickFind: function () {
        var inputNode = this._nodeList['nodeTitle.input'];
        inputNode.didNotSelectSelf();
        var isFindMemeber = this._membersFilter();
        if (isFindMemeber == 0 && inputNode.getString().length != 0) {
            showToast('没有这个玩家');
        }
    },

    /**
     * 点击分配
     * @private
     */
    _clickFenPei: function () {
        showWindow('ClubQuickGroupingWIndow', this._clubId);
    },

    /**
     * 查看分组
     * @private
     */
    _clickSeeGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_See, this._clubId);
    },

    /**
     * 修改组名
     * @private
     */
    _clickChangeGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_ChangeName, this._clubId);
    },

    /**
     * 分组查看
     * @private
     */
    _selectGroupHandler: function (data, type) {
        if (type != GroupWindowType_See) {
            return;
        }
        this._filterGroup = data['type'] || data['pos'];
        this._membersFilter();
    },

    /**
     * 初始化TableView
     */
    initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },


    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubMemberItem', cell, idx, this._clubId, this._usersList[idx]);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData((idx + 1), this._clubId, this._usersList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {

    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1150, 86);
    },
    numberOfCellsInTableView: function (table) {
        return this._usersList.length;
    },

    _memberSettingCallBack: function (data) {
        showWindow('ClubMemberSettingWindow', this._clubId, data);
    },

    /**
     * 过滤条件
     * @private
     */
    _membersFilter: function () {
        var original = this._originalList;
        var userList = original;
        //0是全部显示
        if (this._filterType != 0) {
            userList = [];
            for (var i = 0; i < original.length; i++) {
                var item = original[i];
                if (this._filterType == 1 && item['admins']) {
                    userList.push(item);
                } else if (this._filterType == 2 && item['state'] == 1) {
                    userList.push(item);
                } else if (this._filterType == 3 && item['state'] == 3) {
                    userList.push(item);
                } else if (this._filterType == 4 && item['black']) {
                    userList.push(item);
                }
            }
        }

        //-1 是显示全部分组
        if (this._filterGroup != -1) {
            var tempList = [];
            for (var i = 0; i < userList.length; i++) {
                if (userList[i]['groupPos'] != undefined && userList[i]['groupPos'] == this._filterGroup) {
                    tempList.push(userList[i]);
                }
            }
            userList = tempList;
        }

        var input = this._nodeList['nodeTitle.input'].getString();
        var findUser = [];
        if (input && input.length >= 1) {
            for (var i = 0; i < userList.length; i++) {
                var data = userList[i];
                if (data['name'].indexOf(input) >= 0 || data['uid'].toString().indexOf(input) >= 0) {
                    findUser.push(data);
                }
            }
            if (findUser.length > 0) {
                userList = findUser;
            }
        }
        this._usersList = userList;
        this._tableView.reloadData();
        return findUser.length;
    },
});